package com.lorenzogao.pley.ui.activity

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.lorenzogao.pley.R

import com.lorenzogao.pley.bean.AppInfo
import com.lorenzogao.pley.common.Constant
import com.lorenzogao.pley.common.util.DensityUtil
import com.lorenzogao.pley.ui.base.BaseActivity
import com.lorenzogao.pley.ui.di.component.AppComponent
import com.lorenzogao.pley.ui.fragment.AppDetailFragment
import com.lorenzogao.pley.ui.presenter.AppDetailPresenter
import com.lorenzogao.pley.ui.presenter.contract.AppInfoContract
import kotlinx.android.synthetic.main.activity_app_detail.*
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.backgroundDrawable

class AppDetailActivity : BaseActivity<AppDetailPresenter>(), AppInfoContract.AppDetailView {


    private lateinit var mAppInfo: AppInfo

    override fun setLayout(): Int {
        return R.layout.activity_app_detail
    }

    override fun initData() {
        mAppInfo = intent.getSerializableExtra("appinfo") as AppInfo

        Glide.with(this).load(Constant.BASEURL + mAppInfo.icon).into(img_icon)
        txt_name.text = mAppInfo.displayName


    }


    override fun initView() {


//        too.setNavigationIcon(
//                IconicsDrawable(this)
//                        .icon(Ionicons.Icon.ion_ios_arrow_back)
//                        .sizeDp(16)
//                        .color(resources.getColor(R.color.md_white_1000)
//                        )
//        )

//        mToolBar.setNavigationOnClickListener(View.OnClickListener { finish() })



        var view = mApplication.getView()

        var bitmap = getViewImageCache(view)

        if (bitmap != null) {
            view_temp.backgroundDrawable = BitmapDrawable(bitmap)
        }
        var loaction = IntArray(2)

        view.getLocationOnScreen(loaction)

        var left = loaction[0]
        var top = loaction[1]


        var marginLayoutParams = ViewGroup.MarginLayoutParams(view_temp.layoutParams)

        var params = LinearLayout.LayoutParams(marginLayoutParams)

        params.topMargin = top - DensityUtil.getStatusBarH(this)

        params.leftMargin = left

        params.width = view.width

        params.height = view.height

        view_temp.layoutParams = params

        open()
    }

    override fun setupActivityComponent(appComponent: AppComponent) {


    }


    fun initFramgnet() {
        var fragment = AppDetailFragment(mAppInfo.id)

        var manager = supportFragmentManager

        var transaction = manager.beginTransaction()

        transaction.add(R.id.view_content, fragment)

        transaction.commitAllowingStateLoss()


    }


    override fun showLodading() {

    }

    override fun showError(msg: String) {

    }

    override fun dimissLoading() {

    }


    override fun showResult(appinfo: AppInfo) {

    }


    @SuppressLint("ResourceType")
    private fun open() {


        var height = DensityUtil.getScreenH(this)


        val animator = ObjectAnimator.ofFloat(view_temp, "scaleY", 1f, height.toFloat())

        animator.addListener(object : AnimatorListenerAdapter() {

            override fun onAnimationStart(animation: Animator?) {
                view_temp.backgroundColor = resources.getColor(R.color.background)
            }

            override fun onAnimationEnd(animation: Animator?) {
                view_temp.visibility = View.GONE
                view_coordinator.visibility = View.VISIBLE

                initFramgnet()
            }

        })

        animator.startDelay = 500
        animator.duration = 100
        animator.start()
    }


    private fun getViewImageCache(view: View): Bitmap? {

        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()

        var bitmap: Bitmap? = view.drawingCache ?: return null

        bitmap = Bitmap.createBitmap(bitmap)

        view.destroyDrawingCache()

        return bitmap
    }


}